//Ssa Format

function SSASubtitleDecoder() {
    SubtitleDecoder.call(this);
}
function SSASubtitleDecoder(filePath) {
    SubtitleDecoder.call(this, filePath);
}
SSASubtitleDecoder.prototype = new SubtitleDecoder();
SSASubtitleDecoder.prototype.canDecode = function (fileType) {
    if (fileType == "ssa")
        return true;
    else
        return false;
}
//input "0:15:58.09" output "0:15:58"
SSASubtitleDecoder.prototype.deleteMilliSecond = function (string) {
    var tmpString = string.split('.');
    return tmpString[0];
}
/*
	input Dialogue: 0,0:15:58.09,0:16:02.61,Default,,0000,0000,0000
	["0:15:58.09","0:16:02.61"]
	["0:15:58","0:16:02"]
	output [1558,1602]
*/
SSASubtitleDecoder.prototype.getSubtitleTime = function (string) {
    var time = new Array();
    var tmpTime = new Array();
    var splitString = string.split(',');
    var i = 0;
    /*
	for(i=0;i<1;i++)
	{
		tmpTime[i]=splitString[i+1];
		tmpTime[i]=this.deleteMilliSecond(tmpTime[i]);
		time[i]=this.getNumFromTimeString(tmpTime[i]);
	}*/
    tmpTime[0] = splitString[1];
    tmpTime[0] = this.deleteMilliSecond(tmpTime[0]);
    time[0] = this.getSecondFromTimeString(tmpTime[0]);

    tmpTime[1] = splitString[2];
    tmpTime[1] = this.deleteMilliSecond(tmpTime[1]);
    time[1] = this.getSecondFromTimeString(tmpTime[1]);
    return time;
}
SSASubtitleDecoder.prototype.getContentString = function (string) {
    var splitString = string.split(',');
    var rawContentString = splitString[splitString.length - 1];
    var contentString = rawContentString.split('}');
    return contentString[contentString.length - 1];
}
SSASubtitleDecoder.prototype.readSubtitleFile = function () {
    var tmpString = "";
    var contentString = "";
    var fso, file;
    var time = new Array();
    var index = 0;
    var judgeString = "";
    fso = new ActiveXObject("Scripting.FileSystemObject");
    file = fso.OpenTextFile(this.filePath, 1);
    while (!file.AtEndOfStream) {
        tmpString = file.ReadLine();
        judgeString = tmpString.split(":");
        if (judgeString[0] == "Dialogue") {
            time = this.getSubtitleTime(tmpString);
            contentString = this.getContentString(tmpString);
            this.subtitleArray[index] = new SubtitleEntry(time[0], time[1], contentString);
            index++;
        }
    }
    file.Close();
    return this.subtitleArray;
}
SSASubtitleDecoder.prototype.readSubtitleFromWebPage = function () {
    var table = document.getElementById("subtitletable");
    var tmpString = "";
    var contentString = "";
    var time = new Array();
    var index = 0;
    var judgeString = "";
    var i = 0;
    for (i = 0; i < table.rows.length; i++) {
        //tmpString=table.rows[i].cells[0].innerText;
        tmpString = table.rows[i].cells[0].innerHTML;
        tmpString = this.trimString(tmpString);
        judgeString = tmpString.split(":");
        if (judgeString[0] == "Dialogue") {
            time = this.getSubtitleTime(tmpString);
            contentString = this.getContentString(tmpString);
            this.subtitleArray[index] = new SubtitleEntry(time[0], time[1], contentString);
            index++;
        }
    }
    var array = this.subtitleArray;
    return this.subtitleArray;
}